package ui;

import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;

//import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import java.awt.Color; // 导入 Color 类
import java.awt.Font; // 导入 Font 类
import java.awt.Insets;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

public class ShootingGame extends JFrame {
    private static final int FIXED_WIDTH = 800; // 固定宽度
    public ShootingGame() {
        setTitle("简易射击游戏");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // 获取屏幕尺寸
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenHeight = screenSize.height;
        
        setSize(FIXED_WIDTH, screenHeight);
        
        // 设置窗口在屏幕中间
        int x = (screenSize.width - FIXED_WIDTH) / 2;
        setLocation(x, 0);

        // 初始显示主菜单
        showMainMenu();

        setVisible(true);
    }

    public void showMainMenu() {
// 导入 GridBagLayout 类以解决无法解析的问题
JPanel menuPanel = new JPanel(new GridBagLayout());
// 导入 GridBagConstraints 类以解决无法解析的问题
GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
// 导入 Insets 类以解决无法解析的问题
gbc.insets = new Insets(5, 5, 5, 5);
        menuPanel.setBackground(Color.LIGHT_GRAY); // 设置菜单面板背景颜色

        JButton startButton = new JButton("开始游戏");
        startButton.setBackground(java.awt.Color.GREEN); // 设置按钮背景颜色
        startButton.setForeground(Color.WHITE); // 设置按钮文字颜色
        startButton.setFont(new Font("宋体", Font.BOLD, 16)); // 设置按钮字体
        startButton.addActionListener(e -> showDifficultySelection());

        gbc.gridy++;
        menuPanel.add(startButton, gbc);

        JButton instructionButton = new JButton("使用说明");
        instructionButton.setBackground(Color.BLUE); // 设置按钮背景颜色
        instructionButton.setForeground(Color.WHITE); // 设置按钮文字颜色
        instructionButton.setFont(new Font("宋体", Font.BOLD, 16)); // 设置按钮字体
        instructionButton.addActionListener(e -> showInstructions());

        gbc.gridy++;
        menuPanel.add(instructionButton, gbc);

        setContentPane(menuPanel);
        revalidate();
        repaint();
    }

    private void showDifficultySelection() {
        Object[] options = {"简单", "中等", "困难"};
        int choice = JOptionPane.showOptionDialog(this, "请选择难度", "难度选择",
                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, options, options[0]);

        if (choice != -1) {
            startGame(choice);
        }
    }

    private void showInstructions() {
        JTextArea instructions = new JTextArea("游戏说明：点击鼠标发射子弹，击落敌人得分。敌人到达屏幕底部游戏结束。");
        instructions.setEditable(false);
        JOptionPane.showMessageDialog(this, instructions, "使用说明", JOptionPane.INFORMATION_MESSAGE);
    }

    private void startGame(int difficulty) {
        // 移除菜单面板，添加游戏面板
        GamePanel gamePanel = new GamePanel(this, difficulty);
        setContentPane(gamePanel);
        revalidate();
        repaint();
    }

    public static void main(String[] args) {
        new ShootingGame();
    }
}